
 /*
 * cpe301assignment1.asm
 *
 *  Created: 2/13/2014 2:30:00 PM
 *   Author: Christian Larco
 */ 

 //------------------------------------------------------------------------------------------
 //This are the commands to initialize the values for the program
  .org 0
  
  LDI R26, HIGH(RAMEND) ;Initialize the stack here
  OUT SPH, R26
  LDI R26, LOW(RAMEND)
  OUT SPL, R26

  LDI R17, 50        ;The value that I will store (>30 but <250)
  LDI R18, 20        ;Counter
  
  //------------------------------------------------------------------------------------------

  //------------------------------------------------------------------------------------------
  //This is the looop that generates the random numbers and pushes them into the R17 stack


  CreateStack:
   PUSH R17          ;The value stored in R17 will be pushed int ot he stack 
   INC R17           
   INC R17			 ;We are going to add two to the number saved in R17
   DEC R18			 ;This is the counter that will keep the count of the 20 numbers
   BREQ Donestack  ;Loop to create the Stack with 20 numbers

   check30:
	cpi r17, 30
	brlo plus			; If not greater than 30, jump to plus

						; check greater than 250
	cpi r17, 250
	brsh half

	rjmp CreateStack			; If the number arrived here, it is within range. 

	half:
	lsr r17				; Shift
	cpi r17, 250		; compare to 250
	brsh half			; if still greater, repeat
	rjmp check30

	plus:
	ldi r17, 29			;add 29 if is below 30 
	add r18, r17		;this is were we add both numbers 
	rjmp CreateStack
    
Donestack:
   //------------------------------------------------------------------------------------------

   //------------------------------------------------------------------------------------------
   //This are the commands to initialize and set the Z, Y, X, to point to the stack. Other 
   //registers have been initialized to store the overall sums of the numebers that can be 
   //devided by three and the numbers that can be devided by five. We also initialize a counter 

  LDI ZH, HIGH(RAMEND) ;This sets sets so Z can point to the stack
  LDI ZL, LOW(RAMEND)
  LDI YH, HIGH(RAMEND) ;This sets sets so Y can point to the stack
  LDI YL, LOW(RAMEND)
  LDI XH, HIGH(RAMEND) ;This sets sets so X can point to the stack                      //THIS IS WHERE I LEFT AT
  LDI XL, LOW(RAMEND)
  LDI R20, 0           ;R20 will store the low part of the sums of three
  LDI R21, 0		   ;R21 will store the high part of the sums of three
  LDI R22, 0           ;R22 is used for getting the quotient and storing it
  MOV R10, R22		   ;Saves the contents of R22 to R10
  LDI R23, 0           ;R23 will store the low part of the sums of five
  LDI R24, 0		   ;R24 will store the high part of the sums of five
  LDI R18, 20		   ;Counter to add up the 20 numbers
  MOV R15, R18		   ;Saves the contents of R18 to R15
  MOV R14, R18		   ;Saves the contents of R18 to R14
  LDI R16, 255         ;Used for checking total bits under 16
  LDI R25, 3		   ;loads inmediate 3 into R25 for comparison
  
  //-----------------------------------------------------------------------------------------

  //-----------------------------------------------------------------------------------------
  //This part of the code calculates the whole sum of the numbers and checks that the sum 
  //is under 16 bits which in hex is 0X0316. So we are going to calculate and see if the 
  //numbers that we add are less than 0X0316
  

  Sum:        
  LD R1, Y           ;Command loads the Y poointer into the R1 register
  ADD R3, R1         ;R3 has the lower bit of the TS
  ADC R4,R2          ;R4 has the higher bit of TS          
  DEC YL             ;This pinter will decrease to get the next address
  DEC R18
  BRNE Sum			 ;This will keep going while the counter does not reach zero
  CP R4,R16
  BRLO Approved	  
  
  //-----------------------------------------------------------------------------------------

  //-----------------------------------------------------------------------------------------
  //Here is the loop we are going to use to check if it is divisible by three. We use a successive
  //subtraction in order to see if the number is really divisible by three or five. Then we update
  //the higher and lower registers and add the numbers that are divisible by three together and the
  //numbers that are divisible by five together.

  Approved:
  LD R5, X			 ;We load the X pointer into the R5 register
  MOV R19, R5        ;We use the R19 register as a holder of the original value to check if divisible
  DEC XL             ;Will move to the next address in memory
  DEC R15
  BREQ Five          ;We chekc if the random number is divisible by five 
  CLR R22            ;We clear the contents of R22 
  rJMP DivbyThree 


  DivbyThree:		 ;This is the first loop that will be executed to check for 3 division
  INC R22			 ;This register keeps count of the quuoritent	
  SUB R5, R25		 ;We keep subtracting until it reaches 0 or a negative number
  BRCC DivbyThree	


  ADD R5, R25        ;Here is where we check if the number is divisible
  BREQ ApprovedThree ;Goes to the loop to add the number divisible by three
  DEC R22			 ;Decrements R22 to get the next quotient in line
  BRNE Approved		 ;This is the branch to get the next number on stack
  

  ApprovedThree:	;Here is the loop to add the numbers that are divisible y three.
  ADD R20, R19		;This register contains the numbers that are divisible by three and adds them
  ADC R21, R23      ;R21 contains the second part of the number if it over flows so higher bit
  rJMP Approved


  Five:
  CLR R19			;Clears the register R19
  LDI R19, 5		;Loads 5 into the R19 register
  LD R6, Z			;Loads pointer Z into R6.
  MOV R11, R6		;Copies R6 into R11. This register will be used to save the sum of numbers 
  DEC ZL
  DEC R14			;Counter--
  BREQ Finish		;Send to finish after done processing all operations
  CLR R22
  rJMP DivbyFive	;Send to DivbyFive not really needed

  DivbyFive:		;This is the second part of the program where is cheks if Div by 5
  INC R10			; This adds one to the quotient 
  SUB R6, R19		;Subtract 5 from register 
  BRCC DivbyFive	;send back to loop for consecutive subtractions

  ADD R6, R19		;If r6=0 then it means that the number is divisible by 5 
  BREQ NowCheckFive	;send to the loop to add the numbers
  DEC R10			;Decrement to get our quotient
  BRNE Five			;Gets the next number

  NowCheckFive:
  ADD R23, R11		;Register contains the sum of the numbers divisible by three
  ADC R24, R12		;Register contains the overflow or higher bits of the sum
  rJMP Five


Finish:
JMP Finish
  

